package cn.edu.hqu.javaee.web;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import cn.edu.hqu.javaee.domain.Ingredient;
import cn.edu.hqu.javaee.domain.Ingredient.Type;
import cn.edu.hqu.javaee.domain.Order;
import cn.edu.hqu.javaee.domain.Taco;
import cn.edu.hqu.javaee.repository.IngredientRepository;
import cn.edu.hqu.javaee.repository.TacoRepository;

@Controller
@RequestMapping("/design")
@SessionAttributes("order")
public class DesignTacoController {
  private final IngredientRepository ingredientRepo;
  private TacoRepository tacoRepo;
  @Autowired
  public DesignTacoController(
        IngredientRepository ingredientRepo, 
        TacoRepository tacoRepo) {
    this.ingredientRepo = ingredientRepo;
    this.tacoRepo = tacoRepo;
  }

  @ModelAttribute(name = "order")
  public Order order() {
    return new Order();
  }
  
  @ModelAttribute(name = "design")
  public Taco design() {
    return new Taco();
  }

  @GetMapping
  public String showDesignForm(Model model) {
    List<Ingredient> ingredients = new ArrayList<>();
    ingredientRepo.findAll().forEach(i -> ingredients.add(i));
    
    Type[] types = Ingredient.Type.values();
    for (Type type : types) {
      System.out.println(type.toString().toLowerCase());
      System.out.println(filterByType(ingredients, type));
      model.addAttribute(type.toString().toLowerCase(), 
          filterByType(ingredients, type));      
    }

    return "design";
  }

  @PostMapping
  public String processDesign(
      @Valid Taco design, Errors errors, 
      @ModelAttribute Order order) {

    if (errors.hasErrors()) {
      return "design";
    }
    Taco saved = tacoRepo.save(design);
    order.addDesign(saved);

    return "redirect:/orders/current";
  }

  private List<Ingredient> filterByType(
      List<Ingredient> ingredients, Type type) {
    return ingredients
              .stream()
              .filter(x -> x.getType().equals(type))
              .collect(Collectors.toList());
  }
  protected void formSubmit(HttpServletRequest request,
          HttpServletResponse response)
          throws Exception {
	 response.setCharacterEncoding("UTF=8");
      response.setContentType("text/html;charset=UTF-8");
      //使用request对象的getSession()获取session，如果session不存在则创建一个
      HttpSession session = request.getSession();
      String[] values=request.getParameterValues("ingredients");
      session.setAttribute("data",values);}
}
